<?php

namespace Org\Net;

use Org\Net\WxApi\Wxapi;

class Packet {

    private $wxapi;
    function __construct($config) {
        $this->config = $config;
    }
    public function route($fun, $param = '', $game = '') {
        @require_once "WxApi\oauth2.php";
        $this->wxapi = new Wxapi($this->config);
        switch ($fun) {
            case 'userinfo':
                return self::userinfo($param);
                break;
            case 'wxpacket':
                return self::wxpacket($param, $game);
                break;
            default:
                exit("Error_fun");
        }
    }

    /**
     * 用户信息
     * 
     */
    private function userinfo($param) {
        $get = $param['param'];
        $code = $param['code'];
        if ($get == 'access_token' && !empty($code)) {
            $json = $this->wxapi->get_access_token($code);
            if (!empty($json)) {
                $userinfo = $this->wxapi->get_user_info($json['access_token'], $json['openid']);
                return $userinfo;
            }
        } else {
            //$this->wxapi->get_authorize_url('http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"] . '/index.php/Home/game/wx_hongbao', 'STATE');
            $this->wxapi->get_authorize_url('http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"], 'STATE');
        }
    }

    /**
     * 微信红包
     * 
     */
    private function wxpacket($param, $game) {
        return $this->wxapi->pay($param['openid'], $game);
    }

}